package com.gomtel.smartdevice.ui.activity.tools;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gomtel.smartdevice.R;
import com.gomtel.smartdevice.api.bean.BpResponse;
import com.gomtel.smartdevice.base.WatchAppBaseActivity;
import com.gomtel.smartdevice.bean.UserContent;
import com.gomtel.smartdevice.global.DataClass;
import com.gomtel.smartdevice.global.WatchApplication;
import com.gomtel.smartdevice.moudle.db.entity.BdDataInfo;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.moudle.rxtools.EventBusUtil;
import com.gomtel.smartdevice.ui.dialog.ProgressDialog;
import com.gomtel.smartdevice.ui.dialog.ShowDialog;
import com.gomtel.smartdevice.widget.CalenderUtilsClass;
import com.littlejie.circleprogress.DialProgress;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/26 0026.
 * 血压测试
 */

public class BdActivity extends WatchAppBaseActivity implements View.OnClickListener {

    @BindView(R.id.img_btn_black)
    ImageButton img_btn_black;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.title_about)
    ImageButton title_about;
    @BindView(R.id.begin)
    Button begin;

    @BindView(R.id.hint_layout)
    LinearLayout hint_layout;
    @BindView(R.id.bd_layout)
    LinearLayout bd_layout;

    @BindView(R.id.stop)
    Button stop;
    @BindView(R.id.pvh_mmHg)
    TextView pvh_mmHg;
    @BindView(R.id.ldo_mmHg)
    TextView ldo_mmHg;
    @BindView(R.id.efc_warm_prompt)
    TextView efc_warm_prompt;
    @BindView(R.id.recent)
    TextView recent;
    @BindView(R.id.progress_bar)
    DialProgress progress_bar;


    private String recentlyTime = "--";
    private String pvhMmHg = "--";
    private String ldoMmHg = "--";

    private int ss = 25;
    private int cc = 0;
    private boolean status = false;
    private ShowDialog showDialog;
    private ProgressDialog progressDialog;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (progress_bar != null) {
                        progress_bar.setValue(cc++);
                    }
                    break;
                case 1:
                    if (progress_bar != null) {
                        progress_bar.setValue(ss);
                        initProgressData();
                        stop.setText(getString(R.string.begin));
                    }
                    break;
            }
            return true;
        }
    });

    @Override
    protected void registerEvent(CommonEvent commonevent) {
        switch (commonevent.getCode()) {
            case EventCode.BP_MEASURE_RETURN:
                BpResponse bpResponse = (BpResponse) commonevent.getObject();
                if (bpResponse.getHr() != 0) {
                    dataManager.insertBdDataInfo(new BdDataInfo(UserContent.UserName, CalenderUtilsClass.getCurrentDay(true), String.valueOf(bpResponse.getSystolic()), String.valueOf(bpResponse.getDiastolic())));
                    DataClass.CurrentBP = pvhMmHg + "/" + ldoMmHg + "(mmHg)";
                    EventBusUtil.sendEvent(new CommonEvent(EventCode.ADAPTER_UP_DATA));
                    EventBusUtil.sendEvent(new CommonEvent(EventCode.PULSE_CONDITION));
                    initProgressData();
                } else {
                    toastUtil.showToast("测量失败");
                    stopReceive = true;
                }
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_bd;
    }

    @Override
    protected void initClass() {
        showDialog = ShowDialog.newInstance();
        progressDialog = showDialog.showProgressStatus(this, "准备中测试...");
    }

    @Override
    protected void initData() {
//        NetUtils.getBody("120", "50");
    }

    @Override
    protected void initView() {
        title_about.setVisibility(View.VISIBLE);
        title_name.setText(getString(R.string.BP));
        initProgressData();
    }

    @Override
    protected void initListener() {
        img_btn_black.setOnClickListener(this);
        title_about.setOnClickListener(this);
        begin.setOnClickListener(this);
        stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_about:
                Intent intent = new Intent(BdActivity.this, SignsOfHistoryActivity.class);
                intent.putExtra("ActionType", "1");
                startActivity(intent);
                break;
            case R.id.img_btn_black:
                finish();
                break;
            case R.id.begin:
                if (!DataClass.onlineStatus) {
                    toastUtil.showToast("设备未连接 ... ");
                    return;
                }
                hint_layout.setVisibility(View.GONE);
                bd_layout.setVisibility(View.VISIBLE);
                EventBusUtil.sendEvent(new CommonEvent(EventCode.BP_MEASURE));
                setingProgress();
                break;
            case R.id.stop:
                cc = 0;
                ss = 25;
                if (status) {
                    status = false;
                    stop.setText(getString(R.string.begin));
                } else {
                    setingProgress();
                    stop.setText(getString(R.string.stop));
                    EventBusUtil.sendEvent(new CommonEvent(EventCode.BP_MEASURE));
                }
                break;
        }
    }

    @SuppressLint("SetTextI18n")
    public void initProgressData() {
        List<BdDataInfo> bdDataInfos = dataManager.queryBdDataInfo(UserContent.UserName);
        if (bdDataInfos != null && bdDataInfos.size() > 0) {
            BdDataInfo bdDataInfo = bdDataInfos.get(bdDataInfos.size() - 1);
            recentlyTime = bdDataInfo.getRecentlyTime();
            pvhMmHg = bdDataInfo.getPvhMmHg();
            ldoMmHg = bdDataInfo.getLdoMmHg();
        }
        recent.setText(new StringBuffer().append("近期测量:").append(recentlyTime));
        pvh_mmHg.setText("收缩压\n" + pvhMmHg + "\n(mmHg)");
        ldo_mmHg.setText("舒张压\n" + ldoMmHg + "\n(mmHg)");
    }

    private void ssOrRun() {
        if (stopReceive) {
            return;
        }
        status = true;
        WatchApplication.executorService.submit(() -> {
            try {
                Thread.sleep(999);
                handler.sendEmptyMessage(0);
                ss--;
                if (!DataClass.onlineStatus) {
                    EventBusUtil.sendEvent(new CommonEvent(EventCode.LOCAL_AREA_LINKS));
                }
                if (ss > 0 && status) {
                    ssOrRun();
                } else {
                    status = false;
                    handler.sendEmptyMessage(1);
                    /*if (DataClass.onlineStatus) {
                        toastUtil.showToast("测量成功");
                    } else {
                        toastUtil.showToast("测量失败");
                    }*/
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    private boolean stopReceive = false;

    private void setingProgress() {
        stopReceive = false;
        progressDialog.show();
        WatchApplication.executorService.submit(() -> {
            try {
                Thread.sleep(2999);
                progressDialog.dismiss();
                ssOrRun();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

}
